package com.hao.spinxapi.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.hao.spinxapi.api.SphinxClient;
import com.hao.spinxapi.api.SphinxMatch;
import com.hao.spinxapi.api.SphinxResult;
import com.hao.spinxapi.api.SphinxWordInfo;

public class SphinxUtil {

	static Logger logger  = Logger.getLogger(SphinxUtil.class);
	
	private static SimpleDateFormat dtfmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static long minUnixTime = 0l;
	public static long maxUnixTime = getUnixTimes("2100-01-01 00:00:00");
		
	
	/**
	 * 将日期时间的字符串变为unix时间戳
	 * @param dateTime
	 * @return
	 */
	public static long getUnixTimes(String dateTime) {
		try {
			return dtfmt.parse(dateTime).getTime() / 1000;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	/**
	 * 处理结果
	 * 
	 * @param res
	 * @param cl
	 * @param q
	 */
	public static void print(SphinxResult res, SphinxClient cl, String q) {
		if (res == null) {
			logger.error("Error: " + cl.GetLastError());
			System.exit(1);
		}
		if (cl.GetLastWarning() != null && cl.GetLastWarning().length() > 0){
			logger.warn("WARNING: " + cl.GetLastWarning() + "\n");
		}
		logger.info("Query '" + q + "' retrieved " + res.total + " of " + res.totalFound + " matches in " + res.time
				+ " sec.");
		
		System.out.println("Query stats:");
		for (int i = 0; i < res.words.length; i++) {
			SphinxWordInfo wordInfo = res.words[i];
			System.out.println("\t'" + wordInfo.word + "' found " + wordInfo.hits + " times in " + wordInfo.docs + " documents");
		}
		
		for (int i = 0; i < res.matches.length; i++) {
			SphinxMatch info = res.matches[i];
			StringBuffer sb = new StringBuffer();
			sb.append((i + 1) + ". id=" + info.docId + ", weight=" + info.weight);

			if (res.attrNames == null || res.attrTypes == null)
				continue;

			for (int a = 0; a < res.attrNames.length; a++) {
				sb.append(", " + res.attrNames[a] + "=");

				if (res.attrTypes[a] == SphinxClient.SPH_ATTR_MULTI || res.attrTypes[a] == SphinxClient.SPH_ATTR_MULTI64) {
					sb.append("(");
					long[] attrM = (long[]) info.attrValues.get(a);
					if (attrM != null)
						for (int j = 0; j < attrM.length; j++) {
							if (j != 0)
								sb.append("," + attrM[j]);
						}
					sb.append(")");

				} else {
					switch (res.attrTypes[a]) {
					case SphinxClient.SPH_ATTR_INTEGER:
					case SphinxClient.SPH_ATTR_ORDINAL:
					case SphinxClient.SPH_ATTR_FLOAT:
					case SphinxClient.SPH_ATTR_BIGINT:
					case SphinxClient.SPH_ATTR_STRING:
						/* ints, longs, floats, strings.. print as is */
						sb.append(info.attrValues.get(a));
						break;

					case SphinxClient.SPH_ATTR_TIMESTAMP:
						Long iStamp = (Long) info.attrValues.get(a);
						Date date = new Date(iStamp.longValue() * 1000);
						sb.append(dtfmt.format(date));
						break;

					default:
						sb.append("(unknown-attr-type=" + res.attrTypes[a] + ")");
					}
				}
			}
			System.out.println(sb);
		}
	}

}
